﻿using EasyNet.Componets.Core.Caching;
using Enyim.Caching;
using Enyim.Caching.Memcached;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Cache.Memcached.Enyim
{
    public class MemcachedCache : ICache
    {
        private readonly MemcachedClient _client;
        public MemcachedCache()
        {
            _client = new MemcachedClient();
        }

        public T Get<T>(string key)
        {
            return _client.Get<T>(key);
        }

        public T[] Get<T>(string[] keys)
        {
            var values = _client.Get(keys);

            return values.Select(c => (T)c.Value).ToArray();

        }

        public void Set(string key, object data, int cacheTimeInMinute)
        {
            DateTime expiresAt = DateTime.UtcNow.AddMinutes(cacheTimeInMinute);

            _client.Store(StoreMode.Set, key, data, expiresAt);
        }

        public void Set(string key, object data)
        {
            _client.Store(StoreMode.Set, key, data);
        }

        public void Set(Dictionary<string, object> keyValues)
        {
            throw new NotImplementedException();
        }

        public bool IsSet(string key)
        {
            object data;
            return _client.TryGet(key, out data);
        }

        public void Remove(string key)
        {
            _client.Remove(key);
        }

        public void Clear()
        {
            _client.FlushAll();
        }
    }
}
